package top.wshape1.rpc.loadbalance;

import top.wshape1.rpc.common.entity.ServiceMeta;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Wshape1
 * @version 1.0
 * @description RoundRobinLoadBalance 轮询
 * @since 2024-02-10
 */

public class RoundRobinLoadBalance extends AbstractLoadBalance {

    private static final AtomicInteger current = new AtomicInteger(0);

    private int getCurrent() {
        int i = current.getAndIncrement();
        if (i == Integer.MAX_VALUE)
            current.set(0);
        return i;
    }

    @Override
    protected ServiceMeta doSelect(List<ServiceMeta> serviceMetas) {
        int size = serviceMetas.size();
        return serviceMetas.get(getCurrent() % size);
    }
}
